count the triplets in java with solution